﻿using APICUSTOMCONTROL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace API_CUSTOMCONTROL.Controls
{
    /// <summary>
    /// Interaction logic for MyTextInputControl.xaml
    /// </summary>
    public partial class MyTextInputControl : UserControl
    {
        protected bool isNumber = false;
        protected bool isEmail = false;
        protected bool isRequired = false;

        protected bool isValid;

        protected int minLen = 0;
        protected int maxLen = 50;


        public MyTextInputControl()
        {
            InitializeComponent();
        }

        public void ReFreshAll()
        {
            TextContent = ErrorContent = "";
        }

        #region private function
        private bool CheckRequired()
        {
            isValid = true;
            if (TextContent == "")
            {
                Error.Text = MyMessages.ERROR_REQUIRED;
                isValid = false ;
            }

            return isValid;
        }

        private bool CheckValidEmail()
        {
            isValid = true;
            Error.Text = "";
            if (!InputUtils.IsEmail(TextBox.Text, minLen, maxLen))
            {
                Error.Text = MyMessages.ERROR_EMAIL;
                isValid = false;
            }
            return isValid;
        }

        private bool CheckValidString(bool isCheckMin)
        {
            int length = TextBox.Text.Length;

            Error.Text = "";
            isValid = length <= maxLen || maxLen <= 0;
            if (!isValid)
            {
                Error.Text = String.Format(MyMessages.ERROR_STRING, minLen, maxLen);
                return false;
            }
            if (length < minLen)
            {
                if (isCheckMin)
                {
                    isValid = false;
                }
                Error.Text = String.Format(MyMessages.ERROR_STRING, minLen, maxLen);
            }
            return true;
        }

        private bool CheckValidNumber(char temp)
        {
            isValid = true;
            Error.Text = "";

            if (!char.IsDigit(temp) && !char.IsControl(temp))
            {
                Error.Text = String.Format(MyMessages.ERROR_NUMBER, minLen, maxLen);
                isValid = false;
            }
            return isValid;
        }
        #endregion

        #region public properties
        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }

        public String TextContent
        {
            get { return TextBox.Text; }
            set { TextBox.Text = value; }
        }

        public bool IsRequired
        {
            get { return isRequired; }
            set { isRequired = value; }
        }

        public bool IsNumber
        {
            get { return isNumber; }
            set { isNumber = value; }
        }

        public bool IsEmail
        {
            get { return isEmail; }
            set { isEmail = value; }
        }

        public int MinLength
        {
            get { return minLen; }
            set { minLen = value; }
        }

        public int MaxLength
        {
            get { return maxLen; }
            set { maxLen = value; }
        }

        public String Label
        {
            get { return Title.Text; }
            set { Title.Text = value; }
        }

        public String ErrorContent
        {
            get { return Error.Text; }
            set { Error.Text = value; }
        }

        public Boolean IsReadOnly
        {
            get { return this.TextBox.IsReadOnly; }
            set { this.TextBox.IsReadOnly = value; }
        }
        #endregion

        #region event function
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                return;
            }
            if (isNumber)
            {
                CheckValidNumber((char)KeyInterop.VirtualKeyFromKey(e.Key));
            }
            else
            {
                CheckValidString(false);
            }

            if (!isValid)
            {
                e.Handled = true;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckValidString(true);
            if (isRequired)
            {
                CheckRequired();
            }
            if (isEmail)
            {
                CheckValidEmail();
            }
            if (!isValid)
            {
                e.Handled = true;
            }
        }
        #endregion
    }
}
